﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.UI;
using BusinessLogic.ClassFactory;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;
using Common;

namespace RichECMS.ServerPages.Comment
{
    public partial class Add : Util.BasePage.BasePage
    {
        int intArticleID = -1;
        int intSectionID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Aid"] != null || Request["Sid"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Aid"].ToString()))
                    {
                        int intArticleID = int.Parse(Request["Aid"].ToString());
                        LitPaagePath.Text = "文章评论添加";
                        LitPageName.Text = "文章评论添加";
                    }
                    else if (ParamAuth.IsNumber(Request["Sid"].ToString()))
                    {
                        int intSectionID = int.Parse(Request["Sid"].ToString());
                        LitPaagePath.Text = "栏目评论添加";
                        LitPageName.Text = "栏目评论添加";
                    }
                    else
                    {
                        AlertAndRedirect("进入此页面的方式不正确！", "../CenterMain.aspx", false);
                        return;
                    }
                }
                else
                {
                    AlertAndRedirect("进入此页面的方式不正确！", "../CenterMain.aspx", false);
                    return;
                }
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (!ParamAuth.IsDateTime(SqlCommon.SqlStringFilter(this.txtIssueTime.Value)))
            {
                Alert("请输入正确的发布时间格式");
                bolIsValidate = false;
            }

            if (!ParamAuth.IsNumber(SqlCommon.SqlStringFilter(this.txtOrder.Value)))
            {
                Alert("排序请输入数字");
                bolIsValidate = false;
            }
            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }
            using (DBManager objDBManager = new DBManager())
            {
                objDBManager.BeginTransaction();
                try
                {

                    CommentFactory objFactory = new CommentFactory(objDBManager);
                    BusinessLogic.BusinessRules.Comment objComment = objFactory.NewInstance();
                    //插入评论表
                    objComment.DataItem.chvSummary = this.txtSummary.Value;
                    objComment.DataItem.chvContent = this.txtContent.Value;
                    objComment.DataItem.dtmInTime = Convert.ToDateTime(this.txtIssueTime.Value);
                    objComment.DataItem.chvTitle = this.txtTitle.Value;
                    objComment.DataItem.chvAuthor =State.LoginUser.LoginName ;
                    objComment.DataItem.intSort = int.Parse(this.txtOrder.Value);
                    objComment.DataItem.intArticleID =intArticleID;
                    objComment.DataItem.intSectionID = intSectionID;

                    objComment.DataItem.intVerify = CkboxPublish.Checked == true ? 1 : 0;

                    objComment.DataItem.chvIP = "127.0.0.1";

                    objComment.DataItem.dtmInTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));
                    objComment.Insert();

                    //提交
                    objDBManager.Commit();
                    objFactory.Dispose();
                }
                catch (Exception ex)
                {
                    Alert(ex.Message.ToString().Replace("\r\n", " "));
                    objDBManager.Rollback();
                    return;
                }
            }
            AlertAndRedirect("新增成功！", (intArticleID != -1 ? "List.aspx?Aid=" + intArticleID : (intSectionID != -1 ? "List.aspx?Sid=" + intSectionID : "../CenterMain.aspx")) , false);
        }
    }
}